/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.utility;

import java.util.ArrayList;

import org.openaecfm.model.utility.Table;
import org.openaecfm.model.utility.TableColumn;
import org.openaecfm.model.utility.TableRow;
import org.openaecfm.service.utility.ITableManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class TableIO extends AbstractEntityInstanceInputImpl<Table>
		implements IEntityInstanceOutput<Table> {
	private ITableManager manager;

	public void setManager(ITableManager manager) {
		this.manager = manager;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(Table pojo, IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance("IFCTABLE",
				appDataManager.getName(pojo));

		// Set Name
		if (pojo.getName() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ei.addParameter(new StringParameter(pojo.getName(),
					StringParameter.UTF_8));
		}

		// Set Rows
		if (pojo.getRows() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter rows = new ListParameter();
			for (TableRow r : pojo.getRows()) {
				rows.add(new EntityInstanceNameParameter(appDataManager
						.getName(r)));
			}
			ei.addParameter(rows);
		}

		// Set Columns
		if (pojo.getColumns() == null) {
			ei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter columns = new ListParameter();
			for (TableColumn column : pojo.getColumns()) {
				columns.add(new EntityInstanceNameParameter(appDataManager
						.getName(column)));
			}
			ei.addParameter(columns);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected Table doCreateObject() {
		return new Table();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, Table pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Rows
		Parameter rows = sei.getParameter(1);
		if (rows.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) rows;
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				manager.addRow(pojo,
						(TableRow) appDataManager.getObject(e.toInstanceName()));
			}
		}

		// Set Columns
		Parameter columns = sei.getParameter(2);
		if (columns.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) columns;
			for (Parameter p : l) {
				EntityInstanceNameParameter e = (EntityInstanceNameParameter) p;
				if (pojo.getColumns() == null) {
					pojo.setColumns(new ArrayList<TableColumn>());
				}
				pojo.getColumns().add(
						(TableColumn) appDataManager.getObject(e
								.toInstanceName()));
			}
		}
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei, Table pojo) {
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Name
		Parameter name = sei.getParameter(0);
		if (name.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) name;
			pojo.setName(s.toUTF8());
		}
	}
}
